package com.machine.filling.function.init.OilGunList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseFragment;
import com.machine.filling.db.dao.CardTempDao;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.db.dao.ExceptionOrderDao;
import com.machine.filling.db.dao.GunInfoDao;
import com.machine.filling.db.dao.TempOrderDao;
import com.machine.filling.db.modle.ClientInfoModle;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.init.InputOilGunInfo.InputOilGunInfoFragment;
import com.machine.filling.function.init.login.LoginFragment;
import com.machine.filling.function.main.activity.MainFuelActivity;
import com.machine.filling.net.http.response.GetTokenResBean;
import com.machine.filling.net.http.response.OilGunListResBean;
import com.machine.filling.ui.dialog.NoticeDialog;
import com.machine.filling.util.CustomToast;
import com.machine.filling.util.LogUtil;
import com.machine.filling.util.SpUtils;
import com.machine.filling.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/14 0014.
 * 添加油枪界面
 */

@SuppressLint("ValidFragment")
public class OilGunListFragment extends BaseFragment<OilGunListContract.Presenter> implements OilGunListContract.View, View.OnClickListener, OilGunListAdapter.IClickItemListener {

    private TextView tv_station_name;
    private TextView tv_station_num;
    private TextView tv_station_addr;
    private TextView tv_oil_machine_num;
    private TextView tv_client_num;
    private Button bt_commit;
    private Button bt_reduction;
    private INextFragment INextFragment;
    private RecyclerView recyclerview;
    private List<OilGunListResBean.ResultBean.GunsBean> gunInfoDatas;
    private OilGunListAdapter oilGunListAdapter;
    private NoticeDialog noticeDialog;

    private ClientInfoModle clientInfo;

    public OilGunListFragment(Intent mIntent) {
        super(mIntent);
    }

    @Override
    public View initViews() {
        View view = View.inflate(mContext, R.layout.fg_add_oil_gun, null);
        tv_station_name = (TextView) view.findViewById(R.id.tv_station_name);
        tv_station_num = (TextView) view.findViewById(R.id.tv_station_num);
        tv_station_addr = (TextView) view.findViewById(R.id.tv_station_addr);
        tv_oil_machine_num = (TextView) view.findViewById(R.id.tv_oil_machine_num);
        tv_client_num = (TextView) view.findViewById(R.id.tv_client_num);
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new GridLayoutManager(mContext, 2));
        bt_commit = (Button) view.findViewById(R.id.bt_commit);
        bt_reduction = (Button) view.findViewById(R.id.bt_reduction);
        bt_commit.setOnClickListener(this);
        bt_reduction.setOnClickListener(this);
        bt_reduction.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                getActivity().finish();
                return false;
            }
        });
        return view;
    }

    @Override
    public void initDatas() {
        if (!StringUtil.isNullOrEmpty(ClientInfoDao.getInstance().getModleByDeviceNum().getToken())){
            bt_commit.setText("完  成");
        }else {
            bt_commit.setText("完成初始化");
        }
        clientInfo = ClientInfoDao.getInstance().getModleByDeviceNum();

        LogUtil.e("商家名称-->"+ clientInfo.getStationName());
        LogUtil.e("商家编号-->"+ clientInfo.getStationNum());
        LogUtil.e("商家地址-->"+ clientInfo.getStationAddr());
        LogUtil.e("油机编号-->"+ clientInfo.getMachineNum());
        LogUtil.e("商家名称-->"+ clientInfo.getStationName());

        tv_station_name.setText(clientInfo.getStationName());
        tv_station_num.setText(clientInfo.getStationNum());
        tv_station_addr.setText(clientInfo.getStationAddr());
        tv_oil_machine_num.setText(clientInfo.getMachineNum());
        tv_client_num.setText(clientInfo.getDeviceNum());

        gunInfoDatas = new ArrayList<>();
        oilGunListAdapter = new OilGunListAdapter(mContext, gunInfoDatas);
        oilGunListAdapter.setiClickItemListener(this);
        recyclerview.setAdapter(oilGunListAdapter);
        mPresenter = new OilGunListPresenter(mContext, this);
        mPresenter.getOilGunList();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_commit:
                if (!StringUtil.isNullOrEmpty(ClientInfoDao.getInstance().getModleByDeviceNum().getToken())){
                    getActivity().startActivity(new Intent(mActivity, MainFuelActivity.class));
                    getActivity().finish();
                }else {
                    mPresenter.commitInitInfo();
                }

                break;
            case R.id.bt_reduction:
                //  弹出对话框 确定要还原么？
                noticeDialog = new NoticeDialog(mContext);
                noticeDialog.setMyTitle("提示");
                noticeDialog.setMyContent("系统还原将删除所有油枪及订单信息!\n是否要还原系统？");
                noticeDialog.setiClickListener("确认", "取消", new NoticeDialog.IClickListener() {
                    @Override
                    public void clickLeft() {
                        noticeDialog.dismiss();
                        mPresenter.reSetClient();
                    }

                    @Override
                    public void clickRight() {
                        noticeDialog.dismiss();
                    }
                });
                noticeDialog.show();
                break;
        }
    }

    /**
     * 显示油枪列表
     *
     * @param guns
     */
    @Override
    public void showOilGunListResult(List<OilGunListResBean.ResultBean.GunsBean> guns) {
        gunInfoDatas.clear();
        gunInfoDatas.addAll(guns);
        oilGunListAdapter.notifyDataSetChanged();
    }

    /**
     * 初始化成功完成
     */
    @Override
    public void showCommitInitSuccessResult(GetTokenResBean bean) {
        ClientInfoDao.getInstance().updataToken(bean.getResult().getToken());
        SpUtils.getInstance(mContext).setInitComplete(true);
        CustomToast.showToast(mContext, "初始化完成");
        getActivity().startActivity(new Intent(mActivity, MainFuelActivity.class));
        getActivity().finish();
    }

    /**
     * 系统还原
     */
    @Override
    public void showReSetSuccessResult() {
        // 系统还原，删除数据库
        ClientInfoDao.getInstance().deleteByDeviceNum();//终端信息
        GunInfoDao.getInstance().deleteAll();//油枪信息
        CardTempDao.getInstance().deleteAll();//油卡信息
        TempOrderDao.getInstance().deleteAll();//订单信息
        ExceptionOrderDao.getInstance().deleteAll();//异常订单信息

        SpUtils.getInstance(mContext).setInitComplete(false);
        INextFragment.replaceNext(new Intent(), LoginFragment.class.getName());
    }

    /**
     * 获取油站id
     *
     * @return
     */
    @Override
    public String getStationId() {
        return clientInfo.getStationId();
    }

    /**
     * 获取油机id
     *
     * @return
     */
    @Override
    public String getMationId() {
        return clientInfo.getMachineId();
    }

    /**
     * 获取终端id
     *
     * @return
     */
    @Override
    public String getClientId() {
        return clientInfo.getDeviceId();
    }

    /**
     * 获取油机编号
     *
     * @return
     */
    @Override
    public String getMachineNum() {
        return clientInfo.getMachineNum();
    }

    /**
     * 删除成功
     */
    @Override
    public void showDeleteSuccess(int position) {
        gunInfoDatas.remove(position);
        oilGunListAdapter.notifyDataSetChanged();
    }

    public void setINextFragment(INextFragment INextFragment) {
        this.INextFragment = INextFragment;
    }

    /**
     * 添加 或 修改油枪信息结果
     *
     * @param bean
     */
    @Override
    public void clickModifyOrAdd(OilGunListResBean.ResultBean.GunsBean bean) {
        Intent intent = new Intent();
        if (bean != null) {
            intent.putExtra("gunId", bean.getId());
            intent.putExtra("gunName", bean.getName());
        }
        INextFragment.replaceNext(intent, InputOilGunInfoFragment.class.getName());
    }

    /**
     * 删除油枪
     *
     * @param bean
     */
    @Override
    public void clickDelete(int position,final OilGunListResBean.ResultBean.GunsBean bean) {
        mPresenter.deleteOilGun(position ,bean.getId());
    }
}
